# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：ffmpegModel.py
#日期：2019-07-15
#备注：通过ffmpeg库处理视频信息处理实际遇到的问题(优酷这就是街舞精彩画面：第N季N集(上下))
1.main(q,p)函数入口， F:\PythonProject\machineLearn\\file\jiewu.xls 输入需要截取的画面信息
2.cut_video().save_txt() 保存具体截取的信息到 F:\PythonProject\machineLearn\\file\cut.txt
3.复制保存到cut.txt的信息，调用 one_video()生成截取的画面信息
4.主文件默认在E:\，截取后的文件保存在 E:\video

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import os
import xlrd



def txt_time(file):
    words = ['--']
    data = []
    with open(file, 'r',encoding= 'utf-8') as fp:
        for line in fp.readlines():
            line = line.lstrip().replace(' ','').replace('\n','')
            info = []
            if line.split() and line[0:2] not in words:
                num  = int(line.split('：')[0])           # 数量

                s = line.split('：')[1].split('-->')[0].split('.')           # 开始时间
                s_time = float(s[0]) * 3600 + float(s[1]) * 60 + float(s[2])
                e = line.split('-->')[1].split('.')                          # 结束时间
                e_time = float(e[0]) * 3600 + float(e[1]) * 60 + float(e[2])
                t = e_time - s_time                                          # 持续时间

                info.append(num)
                info.append(s_time)
                info.append(t)
                data.append(info)
                print(num,s,e,e_time,t)
    return(data)


def excel_time(file,q,p,s):
    excel = xlrd.open_workbook(file)             # 打开excle文档读取数据
    table = excel.sheets()[0]
    nrows = table.nrows
    data =[]
    for i in range(2, nrows):           # 第三行开始
        row = []
        qt = table.cell_value(i, 1)     # 第二列开始
        pt = table.cell_value(i, 2)
        st = table.cell_value(i, 3)
        n = table.cell_value(i, 4)
        name = q +'-' + p + '-' + n
        ss = table.cell_value(i, 5)
        t = table.cell_value(i, 6)
        #print(name,ss,t)
        if q == qt and pt == p and st==s and n>'0':    # qt-N季  pt-N集,s-上下, n控制截取多少
            row.append(name)
            row.append(ss)
            row.append(t)
            data.append(row)
    return(data)


def get_image(s_time, e_time, video_path, output_path):
    t = int((e_time - s_time)*2) + 1     # 算头算尾
    for i in range(1,t+1):
        os.system('ffmpeg -y -i %s -f image2 -ss %s -frames 1 %s.jpg' % (video_path, s_time, output_path + str(i)))
        s_time += 0.5


# 传入视频开始时间、持续时间
def cut_video(name, ss, t, video_path, output_path):
    out_file = output_path + name + '.mp4'
    #print(ss,t,out_file)
    #os.system('ffmpeg -i %s -ss %s -t %s -c:a copy -c:v copy -y %s' % (video_path, ss,t,out_file))
    save_txt('ffmpeg -i %s -ss %s -t %s -c:a copy -c:v copy -y %s' % (video_path, ss,t,out_file))
    #  4 56 4 60

# 声音先，往前
def one_video():   # libx264 2609kps
    os.system('ffmpeg -i E:\\05-02-2.mp4 -ss 7759.8 -t 88.2 -c:a copy -c:v copy -y E:\\video\\05-02-19.mp4')

def save_txt(word):
    with open('F:\PythonProject\machineLearn\\file\cut.txt', 'a',encoding= 'utf-8') as fp:
        fp.write(word+'\n')


# 第N季N集
def main(q,p,s):
    #info = txt_time('file\jiewu.txt')
    file = 'E:\\' + q + '-' + p + '-' + s + '.mp4'  # q-期数，p-集
    info = excel_time('F:\PythonProject\machineLearn\\file\jiewu.xls',q,p,s)
    for time in info:
        name = time[0]
        ss = time[1]
        t = time[2]
        cut_video(name, ss, t, file, 'E:\\video\\')


if __name__ == '__main__':
    #main('05','02','2')    # 第N季N集(上-1，下-2，否则'0')
    one_video()
    # get_image(100.5, 102.5,'F:\9.mp4','F:\image\\')
    #excel_time('file\jiewu.xls','05','01')